﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace EasySL.Controls
{
    public class ExScrollBarBlock : Grid
    {
        public Action<double> OnValueChange { get; set; }
        public Action<double> OnValueChanging { get; set; }

        public ExScrollBarBlock()
            : base()
        {
            this.Loaded += new RoutedEventHandler(ExScrollBarBlock_Loaded);
        }

        void ExScrollBarBlock_Loaded(object sender, RoutedEventArgs e)
        {
            Panel panel = this.Parent as Panel;
            Thumb thumb = panel.FindName("VerticalThumb") as Thumb;
            thumb.DragCompleted += new DragCompletedEventHandler(thumb_DragCompleted);
            thumb.DragStarted += new DragStartedEventHandler(thumb_DragStarted);
            FrameworkElement element = this;

            while (element != null && !(element is ScrollBar))
                element = VisualTreeHelper.GetParent(element) as FrameworkElement;

            if (element is ScrollBar)
            {
                scrollBar = element as ScrollBar;
                scrollBar.ValueChanged += new RoutedPropertyChangedEventHandler<double>(scrollBar_ValueChanged);
            }
            
            while (element != null && !(element is ExDataGrid))
                element = VisualTreeHelper.GetParent(element) as FrameworkElement;

            if (element is ExDataGrid)
            {
                ExDataGrid exDataGrid = element as ExDataGrid;
                exDataGrid.AddScrollBar(this);
            }
        }

        void thumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            dragging = true;
        }

        void scrollBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            ValueChanging();
            
            if (!dragging)
            {
                ValueChange();
            }

        }

        void thumb_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            dragging = false;
            ValueChange();
        }

        private void ValueChanging()
        {
            if (this.OnValueChanging != null && scrollBar != null)
                this.OnValueChanging(scrollBar.Value);
        }
        
        private void ValueChange()
        {
            if (this.OnValueChange != null && scrollBar != null)
                this.OnValueChange(scrollBar.Value);
        }

        public void Init()
        {
            if (this.scrollBar != null)
                scrollBar.Value = 0;
        }

        public void SetViewportSize(double value)
        {
            scrollBar.ViewportSize = value;
        }

        public void Enable(bool isEnable)
        {
            this.scrollBar.Visibility = isEnable ? Visibility.Visible : Visibility.Collapsed;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        private bool dragging;
        private ScrollBar scrollBar;
    }
}
